﻿using System;
using System.Collections.Generic;
using System.Web;
using HZ.BidSystem.Model;
using System.Web.UI;

/// <summary>
/// 当前用户上下文
/// </summary>
public static class CurrentAccount
{
    const string SESSION_NAME = "Account";
    static readonly string SESSION_BIDPWD = "BidPwd";
    static readonly string SESSION_RANDPWD = "RandPwd";
    static readonly string SESSION_PROJECTID = "ProjectId";
    static readonly string SESSION_ORGNAME = "OrgName";
    private const string VALIDATE_CODE = "NUM_CODE";
    /// <summary>
    /// 当前账户信息
    /// </summary>
    public static Account Account
    {
        get
        {
            return HttpContext.Current.Session[SESSION_NAME] as Account;
        }
        set
        {
            HttpContext.Current.Session[SESSION_NAME] = value;
        }
    }

    /// <summary>
    /// 报价密码缓存
    /// </summary>
    public static string BidPwd
    {
        get
        {


            return (string)HttpContext.Current.Session[SESSION_BIDPWD];
        }
        set
        {
            HttpContext.Current.Session[SESSION_BIDPWD] = value;
        }
    }

    /// <summary>
    /// 随机密码缓存
    /// </summary>
    public static string RandPwd
    {
        get
        {
            return (string)HttpContext.Current.Session[SESSION_RANDPWD];
        }
        set
        {
            HttpContext.Current.Session[SESSION_RANDPWD] = value;
        }
    }



    /// <summary>
    /// 机构名
    /// </summary>
    public static string OrgName
    {
        get
        {
            string _OrgName;
            if (HttpContext.Current.Session[SESSION_ORGNAME] == null)
            {

                if (Account.AccountType.Value == 2)
                {

                    _OrgName = BLL_Factory.IBLL_Common.GetCompanyById(Account.AccountId.Value).CompanyName;
                }
                else
                {
                    _OrgName = "系统管理员";
                }

                HttpContext.Current.Session[SESSION_ORGNAME] = _OrgName;
            }
            else
            {
                _OrgName = (string)HttpContext.Current.Session[SESSION_ORGNAME];
            }


            return _OrgName;
        }
    }

    /// <summary>
    /// 获取当前项目编号
    /// </summary>
    public static Int32 ProjectId
    {
        get
        {
            return (Int32)HttpContext.Current.Session[SESSION_PROJECTID];
        }
        set
        {
            HttpContext.Current.Session[SESSION_PROJECTID] = value;
        }
    }

    /// <summary>
    /// 获取当前项目信息
    /// </summary>
    public static Project Project
    {
        get
        {
            return BLL_Factory.IBLL_Common.GetProjectInfoById(ProjectId);
        }
    }

    /// <summary>
    /// 获取当前用户开关上下文
    /// </summary>
    public static Company_Project_Switch Company_Project_Switch
    {
        get
        {
            return BLL_Factory.IBLL_Company.GetCompanySwitch(Account.AccountId.Value, ProjectId);
        }
    }

    /// <summary>
    /// Session是否过期
    /// </summary>
    public static bool IsTimeOut
    {
        get
        {
            return HttpContext.Current.Session[SESSION_NAME] == null;
        }
    }

    public static void CheckSessionTimeOut(Page page)
    {
        if (IsTimeOut)
        {
            HttpContext.Current.Response.Redirect("~/Timeout.htm");
        }

        // 检查是否被系统踢下线
        if (HZ.BidSystem.BLL.BLL_Cache.ForbidLogin.ContainsKey(CurrentAccount.Account.AccountId.Value))
        {
            if (CurrentAccount.Account.LastLogin.Value < HZ.BidSystem.BLL.BLL_Cache.ForbidLogin[CurrentAccount.Account.AccountId.Value])
            {
                HttpContext.Current.Response.Redirect("~/ForceReLogin.htm");
            }

        }

        var ia = page as IAccess;
        if (ia != null)
        {
            if (Account.AccountType.Value == 2)
            {

                foreach (var item in ia.AccessList)
                {
                    if ((item & EnumAccess.Company) == EnumAccess.Company)
                    {
                        return;
                    }

                }
            }
            if (Account.AccountType.Value == 1 || Account.AccountType.Value == 0)
            {
                foreach (var item in ia.AccessList)
                {
                    if ((item & EnumAccess.Manage) == EnumAccess.Manage)
                    {
                        return;
                    }

                }
            }

            HttpContext.Current.Response.Redirect("~/NoPermission.htm");
        }
    }

    public static string CheckCode
    {
        get
        {
            return HttpContext.Current.Request.Cookies["CheckCode"].Value;
        }
    }

    /// <summary>
    /// 是否设置报价密码
    /// </summary>
    public static bool IsSetBidPwd
    {
        get
        {
            return !string.IsNullOrEmpty(Company_Project_Switch.BidPassword);
        }
    }

    /// <summary>
    /// 是否经过验证报价密码
    /// </summary>
    public static bool IsCheckBidPwd
    {
        get
        {
            return !string.IsNullOrEmpty(BidPwd);
        }
    }
    /// <summary>
    /// 是否确认报价
    /// </summary>
    public static bool IsConfirmBid
    {
        get
        {
            return Company_Project_Switch.ConfirmStatus ?? false;

        }
    }

    /// <summary>
    /// 是否解密报价
    /// </summary>
    public static bool IsDecPrice
    {
        get
        {
            return Company_Project_Switch.DecStatus ?? false;
        }
    }

    /// <summary>
    /// 是否在竞价时间中
    /// </summary>
    private static bool InCompeteTime
    {
        get
        {
            var list = BLL_Factory.IBLL_Manage.GetCompeteTimeConfigByProjectid(ProjectId);
            bool incompetetime = false;
            foreach (var item in list)
            {
                if (item.CompeteEndTime != null && item.CompeteStartTime != null)
                {
                    // 在竞价时间中
                    if (DateTime.Now <= item.CompeteEndTime.Value && DateTime.Now >= item.CompeteStartTime.Value)
                    {
                        incompetetime = true;
                        break;
                    }
                }
            }
            return incompetetime;
        }
    }
    /// <summary>
    /// 当前竞价轮次
    /// </summary>
    public static int CurrentCompeteTimes
    {
        get
        {
            var list = BLL_Factory.IBLL_Manage.GetCompeteTimeConfigByProjectid(ProjectId);

            int times = -1;
            foreach (var item in list)
            {
                if (item.CompeteEndTime != null && item.CompeteStartTime != null)
                {
                    // 在竞价时间中
                    if (DateTime.Now <= item.CompeteEndTime.Value && DateTime.Now >= item.CompeteStartTime.Value)
                    {
                        times = item.CompeteTime.Value;
                        break;
                    }

                    // 过了竞价时间
                    if (DateTime.Now > item.CompeteEndTime.Value)
                    {
                        times = item.CompeteTime.Value;
                        continue;
                    }
                }
            }
            return times;
        }
    }

    /// <summary>
    /// 验证码校验函数
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public static bool VerifyValidateCode(string code)
    {
        if (!string.IsNullOrEmpty(code))
        {
            if (HttpContext.Current.Session[code] != null)
            {
                if (HttpContext.Current.Session[code].ToString() == code)
                {
                    //验证成功清空Session
                    HttpContext.Current.Session[code] = null;
                    return true;
                }

            }
        }
        //验证失败清空Session
        HttpContext.Current.Session[code] = null;
        return false;
    }

    /// <summary>
    /// 设置验证码
    /// </summary>
    /// <param name="code"></param>
    public static void SetValidateCode(string code)
    {
        HttpContext.Current.Session[code] = code;
    }

    /// <summary>
    /// 获取客户端IP地址
    /// </summary>
    public static string ClientIP { get { return HZ.Fun.GetClientIP(); } }
}
